<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <link type="text/css" rel="stylesheet" href="/webjars/bootstrap/3.3.7/css/bootstrap.css">
    <script type="text/javascript" src="/webjars/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript" src="/webjars/vue/2.6.10/dist/vue.js"></script>
    <script type="text/javascript" src="/webjars/axios/0.19.0/dist/axios.js"></script>
    <script type="text/javascript" src="/webjars/bootstrap/3.3.7/js/bootstrap.js"></script>

</head>
<body>
<div id="vuebox">
    <div class="row">
        <div class="col-md-6">
            <form class="form-inline" role="form" style="padding-left: 250px;">
                <div class="form-group" style="padding-left: 20px;">
                    <label>查找联系人:</label>
                    <input v-model="cname"  type="text" class="form-control">
                    <!--<select class="form-control" v-model="categoryid">
                        <option value="0">全部</option>
                        <option v-for="item in categoryList.data" :value="item.id">{{item.name}}</option>
                    </select>-->
                </div>
                <div class="form-group"  style="padding-left: 20px;">
                    <button type="button" class="btn btn-primary" @click="selectContact(1)">查询</button>
                </div>

            </form>
        </div>
        <div class="col-md-4 text-right">
            <a @click="showModal()" class="btn btn-primary">添加联系人</a>
        </div>
    </div>
    <br>
    <table class="table table-bordered container">
        <thead>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>手机</th>
            <th>QQ</th>
            <th>邮箱</th>
            <th>地址</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="item in contactList.data.list">
            <td>{{item.id}}</td>
            <td><a @click="updateEntry(item.id)">{{item.cname}}</a></td>
            <td>{{item.mobile}}</td>
            <td>{{item.qq}}</td>
            <td>{{item.email}}</td>
            <td>{{item.address}}</td>
        </tr>
        </tbody>
    </table>
    <br>
    <div class="text-center">
        <ul class="pagination">
            <li v-show="contactList.data.hasPreviousPage">
                <a href="#" @click.stop="selectContact(contactList.data.perPage)">&laquo;</a>
            </li>
            <li :class="{active:contactList.data.pageNum==i}" v-for="i of contactList.data.navigatepageNums">
                <a href="#" @click.stop="selectContact(i)">{{i}}</a>
            </li>
            <li v-show="contactList.data.hasNextPage">
                <a href="#" @click.stop="selectContact(contactList.data.nextPage)">&raquo;</a>
            </li>
        </ul>
    </div>

    <br>
    <!--模态框-->
    <div class="modal" id="mymodal">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" type="button" >&times;</button>
                    <div class="modal-title">
                        添加联系人
                    </div>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <input type="text" id="contact_id" v-model="new_contact.id">
                        <div class="form-group">
                            <label for="cname" class="col-sm-2 control-label">名称:</label>
                            <div class="col-sm-10">
                                <input v-model="new_contact.cname" id="cname" type="text" class="form-control" placeholder="请输入名称">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="mobile" class="col-sm-2 control-label">手机:</label>
                            <div class="col-sm-10">
                                <input v-model="new_contact.mobile" id="mobile" type="text" class="form-control" placeholder="请输入手机"/>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="qq" class="col-sm-2 control-label">QQ:</label>
                            <div class="col-sm-10">
                                <input v-model="new_contact.qq" id="qq" type="text" class="form-control" placeholder="请输入QQ">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="email" class="col-sm-2 control-label">邮箱:</label>
                            <div class="col-sm-10">
                                <input v-model="new_contact.email" id="email" type="text" class="form-control" placeholder="请输入邮箱">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="address" class="col-sm-2 control-label">地址:</label>
                            <div class="col-sm-10">
                                <input v-model="new_contact.address" id="address" type="email" class="form-control" placeholder="请输入地址">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button class="btn btn-default" @click="hideModal(),clearForm()">关闭</button>
                    <button class="btn btn-primary" @click="saveModal()">保存</button>
                </div>
            </div>
        </div>
    </div>



</div>
</body>
</html>
<script type="text/javascript">
    var vm =new Vue({
        el:"#vuebox",
        data:{
            contactList:{},
            cname:"",
            pageNum:'',
            saveResult:{},
            new_contact:{id:null,cname:"",mobile:"",qq:"",email:"",address:""}
        },
        mounted(){
            this.selectContact(1);
        },
        methods:{
            selectContact:function (pageNum) {
                axios.get("/selectContact",{params:{cname:this.cname,pageNum:pageNum}}).then
                (res=>{
                    this.contactList=res.data
                    this.pageNum=pageNum
                });
            },
          /*  selectContact: function (pageNum) {
                axios({url:"/selectContact", method:"post",
                    params:{cname:this.cname,pageNum:pageNum}})
                    .then(res=>{
                        this.contactList=res.data.data
                        this.pageNum=pageNum;
                    });
            },*/
            showModal:function () {
                $('#mymodal').modal('show');
            },
            hideModal:function () {
                $('#mymodal').modal('hide');
            },
            clearForm:function () {
                $('#contact_id').val('');
                $('#cname').val(0);
                $('#mobile').val('');
                $('#qq').val('');
                $('#email').val('');
                $('#address').val('');
            },
            saveModal:function () {
                axios.post("/saveContact",this.new_contact).then(res=>{
                    this.saveResult=res.data
                    if(this.saveResult.result=='SUCCESS'){
                        this.selectContact(this.pageNum);
                        this.hideModal();
                        this.clearForm();
                    }else{
                        alert(this.saveResult.message)
                    }
                });
            },
            updateEntry:function (id) {
                axios.get("/getContactById",{params:{id:id}}).then(res=>{
                    this.new_contact=res.data.data;
                    this.showModal();
                })
            }

        }

    });
</script>